package github.leavesczy.reactivehttpsamples.widget.dialog;

import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

import github.leavesczy.reactivehttpsamples.R;
import github.leavesczy.reactivehttpsamples.adapter.BridgeSampleSelectAdapter;
import github.leavesczy.reactivehttpsamples.core.mode.MeasureModelData;
import github.leavesczy.reactivehttpsamples.widget.dialog.base.BaseDialogFragment;
import github.leavesczy.reactivehttpsamples.widget.dialog.base.DialogLayoutCallback;


public class BridgeSampleSelectDialog extends BaseDialogFragment {


    private RecyclerView rvSampleSelect;
    private TextView tvOk;
    private TextView tvCancel;

    private BridgeSampleSelectAdapter adapter = new BridgeSampleSelectAdapter();

    public BridgeSampleSelectDialog init(Context context, List<MeasureModelData> lists, List<Long> currentIds,
                                         Callback callback) {
        super.init(context, new DialogLayoutCallback() {
            @Override
            public int bindTheme() {
                return R.style.CommonContentDialogStyle;
            }

            @Override
            public int bindLayout() {
                return R.layout.dialog_bridge_sample_select;
            }

            @Override
            public void initView(final BaseDialogFragment dialog, View contentView) {
                rvSampleSelect = contentView.findViewById(R.id.rvSampleSelect);

                tvOk = contentView.findViewById(R.id.tvOk);
                tvCancel = contentView.findViewById(R.id.tvCancel);

                ClickUtils.applyPressedBgDark(tvOk);
                ClickUtils.applyPressedBgDark(tvCancel);
                tvOk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        List<Long> selectIds = adapter.getSelectIds();
                        if (CollectionUtils.isEmpty(selectIds)) {
                            ToastUtils.showShort("请选择样本");
                            return;
                        }

                        dismiss();
                        callback.ok(selectIds);
                    }
                });

                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                        callback.cancel();
                    }
                });


                rvSampleSelect.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
                rvSampleSelect.setAdapter(adapter);

                adapter.initSelectIds(currentIds);
                adapter.addAll(lists);
            }

            @Override
            public void setWindowStyle(Window window) {
            }

            @Override
            public void onCancel(BaseDialogFragment dialog) {

            }

            @Override
            public void onDismiss(BaseDialogFragment dialog) {

            }
        });
        return this;
    }

    public interface Callback {
        void ok(List<Long> ids);

        void cancel();
    }
}